package xrate

// 获取用户距离保底的次数
//
//	uid	用户ID
//	bid	批次ID
//	pid	产品ID
//	sid	星级ID
func UserCount(uid uint, bid, pid, sid uint) uint {
	var id_old uint
	if _default.DB == nil {
		return 0
	}
	db := _default.DB.
		Table(_default.UserName).
		Where("user_id", uid).
		Where("batch_id", bid)
	if pid > 0 {
		db = db.Where("project_id", pid)
	}
	if sid > 0 {
		db = db.Where("start_id", sid)
	}
	err := db.Select("id").Find(&id_old).Error
	if err != nil {
		return 0
	}
	var count int64
	err = _default.DB.
		Table(_default.UserName).
		Where("user_id", uid).
		Where("batch_id", bid).
		Where("id > ?", id_old).
		Count(&count).
		Error
	if err != nil {
		return 0
	}
	return uint(count)
}
